'use strict'

$(function () {
    console.log('球球你快出来吧');
    query()

})

function query() {
    let keyword=$('#keyword').val();
    getJsonResult(keyword).then(data=>{
        if (keyword) {
            let arr=data.filter(item=>{ 
                return item.id==keyword || item.title==keyword || item.zhaiyao==keyword || item.neirong==keyword || item.fenlei==keyword ||
                item.author==keyword || item.time==keyword
             })
             console.log(arr);
             renderFn(arr)
        }else{
             renderFn(data)
        }
    })
}

function getJsonResult(keyword) {
    return new Promise(function (reslove,reject) {
        $.get('http://localhost:8000/blog?keyword='+keyword,data=>{
            
            reslove(data);
          
        })
        
    })
}
function renderFn(arr) {
    let row=$('.rowData');
    row.remove();
    arr.filter(item=>{
        let html=`
        <tr class="rowData">
        <td>${item.id}</td>
        <td>${item.title}</td>
        <td>${item.zhaiyao}</td>
        <td>${item.neirong}</td>
        <td>${item.fenlei}</td>
        <td>${item.author}</td>
        <td>${item.time}</td>
        <td>
                 <input type="button" value="修改" onclick="update(${item.id})">
                 <input type="button" value="删除" onclick="del(${item.id})">
             </td>
    </tr>
        `
        $(`#tbData`).append(html);
    })
}

function update() {
    location.href='./addOrEdit.html';
    
    }
    $.ajax({
        type: "put",
        contenType: "application/json",
        url: `http://localhost:8000/blog`,
        data: obj,
        success: function (arr) {
            console.log(arr);
            renderFn(arr)
            window.location.href = './index.html';
        }
    })



function del(id) {
    delBlogById(id).then(data=>{
        console.log(data);
    })
}
function add(obj) {
    location.href='./addOrEdit.html';
    postBlog(obj).then(data=>{
   console.log(data);
    })
}